package com.edward.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.annotation.Secured;

import com.edward.JsonDateProcessor;
import com.edward.PagerModel;
import com.edward.model.RunCost;
import com.edward.service.RunCostManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class RunCostAction extends ActionSupport{


	private static final long serialVersionUID = -2531663086591196847L;

	private RunCost runCost;
	
	private RunCostManager runCostManager;
	
	private String query="";
	
	private String sd;
	
	private int limit;
	
	private int start;
	
	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public String getSd() {
		return sd;
	}

	public void setSd(String sd) {
		this.sd = sd;
	}

	public RunCost getRunCost() {
		return runCost;
	}

	public void setRunCost(RunCost runCost) {
		this.runCost = runCost;
	}

	public void setRunCostManager(RunCostManager runCostManager) {
		this.runCostManager = runCostManager;
	}
	
	@Secured("ROLE_FINANCE")
	public String add() throws Exception{
	
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		RunCost r = runCost;
		DateFormat format = new SimpleDateFormat("yyyy-MM");         
		runCost.setDate(format.parse(sd));
		runCost.setBuySum(runCost.getCount() * runCost.getPrice());
		runCost.setSum(r.getArk() + r.getBonus() + r.getBuySum() + r.getCard()
				+ r.getCustoms() + r.getElec() + r.getFreight() + r.getInsure()
				+ r.getOthers() + r.getPresscar() + r.getSeal() + r.getStack()
				+ r.getTrailer());
		runCostManager.addRunCost(runCost);
		runCostManager.addcheck(runCost);
		response.getWriter().print("{success : true , msg : '添加成功!'}");
		return null;
	}

	
	@Secured("ROLE_FINANCE")
	public String update() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		DateFormat format = new SimpleDateFormat("yyyy-MM");         
		runCost.setDate(format.parse(sd));
		runCost.setBuySum(runCost.getCount() * runCost.getPrice());
		runCost.setSum(runCost.getArk() + runCost.getBonus() + runCost.getBuySum() + runCost.getCard()
				+ runCost.getCustoms() + runCost.getElec() + runCost.getFreight() + runCost.getInsure()
				+ runCost.getOthers() + runCost.getPresscar() + runCost.getSeal() + runCost.getStack()
				+ runCost.getTrailer());
		RunCost r = runCostManager.getRunCost(runCost.getId());
		RunCost rc  = runCostManager.getSum(runCost.getDate());
		rc.setArk(rc.getArk() + runCost.getArk() - r.getArk());
		rc.setBonus(rc.getBonus() + runCost.getBonus() - r.getBonus());
		rc.setBuySum(rc.getBuySum() + runCost.getBuySum() - r.getBuySum());
		rc.setCard(rc.getCard() + runCost.getCard() - r.getCard());
		rc.setCustoms(rc.getCustoms() + runCost.getCustoms() - r.getCustoms());
		rc.setElec(rc.getElec() + runCost.getElec() - r.getElec());
		rc.setFreight(rc.getFreight() + runCost.getFreight() - r.getFreight());
		rc.setInsure(rc.getInsure() + runCost.getInsure() - r.getInsure());
		rc.setOthers(rc.getOthers() + runCost.getOthers() - r.getOthers());
		rc.setPresscar(rc.getPresscar() + runCost.getPresscar() - r.getPresscar());
		rc.setSeal(rc.getSeal() + runCost.getSeal() - r.getSeal());
		rc.setStack(rc.getStack() + runCost.getStack() - r.getStack());
		rc.setTrailer(rc.getTrailer() + runCost.getTrailer() - r.getTrailer());
		rc.setSum(rc.getSum() + runCost.getSum() - r.getSum());
		runCostManager.updateRunCost(rc);
		runCostManager.updateRunCost(runCost);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String delete() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		RunCost r = runCostManager.getRunCost(runCost.getId());
		RunCost rc  = runCostManager.getSum(r.getDate());
		rc.setArk(rc.getArk() - r.getArk());
		rc.setBonus(rc.getBonus() - r.getBonus());
		rc.setBuySum(rc.getBuySum() - r.getBuySum());
		rc.setCard(rc.getCard() - r.getCard());
		rc.setCustoms(rc.getCustoms() - r.getCustoms());
		rc.setElec(rc.getElec() - r.getElec());
		rc.setFreight(rc.getFreight() - r.getFreight());
		rc.setInsure(rc.getInsure() - r.getInsure());
		rc.setOthers(rc.getOthers() - r.getOthers());
		rc.setPresscar(rc.getPresscar() - r.getPresscar());
		rc.setSeal(rc.getSeal() - r.getSeal());
		rc.setStack(rc.getStack() - r.getStack());
		rc.setTrailer(rc.getTrailer() - r.getTrailer());
		rc.setSum(rc.getSum() - r.getSum());
		runCostManager.updateRunCost(rc);
		runCostManager.deleteRunCost(runCost.getId());
		response.getWriter().print("{success : true , msg : '删除成功!'}");
		return null;
	}

	@Secured("ROLE_FINANCE")
	public String getList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
	    pager = runCostManager.pagerList(start, limit,query);
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String getItems() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
		DateFormat format = new SimpleDateFormat("yyyy-MM");         
	    pager = runCostManager.pagerItems(start, limit,format.parse(sd));
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	
	
	
	@Secured("ROLE_FINANCE")
	 public String getListJudge() throws Exception {
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print("{success : true , msg : '成功!',deny:false}");
		 return null;
	 }
	
	
	
	
	
	
	

}
